import { $get, $post, $put } from '@js/authRequest'
import { message } from 'antd'

export async function getCauseRequest (n) {
  const condition = []
  if (n) {
    condition.push({ k: 'value', o: 'like', v: `%${n}%` })
  }
  const res = await $get('/api/common/causeaction/list', {
    condition: JSON.stringify(condition)
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取案由树
export async function getTreeData () {
  const res = await $get('/api/common/cause_action')
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

export async function addRequest (d) {
  d.status = '已提交'
  const res = await $post('/api/tiaojie/case', d)
  if (res.status !== 1) {
    message.error(res.message)
    throw new Error(res.message)
  }
  return res.data
}

export async function updateRequest (d) {
  const id = d.id
  delete d.id
  const res = await $put(`/api/tiaojie/case/${id}`, d)
  if (res.status !== 1) {
    message.error(res.message)
    throw new Error(res.message)
  }
  return res.data
}

export async function getRequest (id) {
  const res = await $get(`/api/tiaojie/case/${id}`)
  if (res.status !== 1) {
    message.error(res.message)
    throw new Error(res.message)
  }
  return res.data
}
